package com.anbao.action;

import java.util.ArrayList;
import java.util.List;

import com.anbao.dataBean.MessageBaseBean;
import com.anbao.util.MessageCommonData;
import com.anbao.util.NumberUtil;

public class MessageReadAction {

	public void readMessageInfo(List<Byte> data) {

		// 数据的长度
		int length = data.get(1) * 256 + data.get(0);

		// 判断数据的合理性
		if (length != data.size()) {
			return;
		}

		// 通信命令的版本号
		int messageVersion = data.get(2);

		// 用户类型
		int userType = data.get(3);
		// 消息类型
		int messageType = data.get(5) * 256 + data.get(4);
		// 消息序号
		int messageNumber = data.get(7) * 256 + data.get(6);

		byte[] usrId_array = new byte[8];

		for (int i = 0; i < 8; i++) {
			usrId_array[i] = data.get(8 + i);
		}

		long usrId = NumberUtil.bytesToLong(usrId_array);

		MessageBaseBean baseBean = new MessageBaseBean();
		baseBean.messageLength = length;
		baseBean.messageNumber = messageNumber;
		baseBean.messageType = messageType;
		baseBean.messageVersion = messageVersion;
		baseBean.usrid = usrId;
		baseBean.usrType = userType;

		List<Byte> messageData = new ArrayList<Byte>(data.subList(16,
				data.size()));

		messageClassify(baseBean, messageData);

	}

	/**
	 * 消息分类处理
	 * @param baseBean
	 * @param messageData
	 */
	private void messageClassify(MessageBaseBean baseBean,
			List<Byte> messageData) {
		
		
		//判断数据的合理性
		if(!cheakeData(baseBean)){
			
			return;
		}
		
		        

		switch (baseBean.messageType) {

		case MessageCommonData.headCmd_rsp.HEARTBEAT_RSP:
			//心跳响应
			
//			LoginService.getLoginService().setLoginResult(baseBean, messageData);
			
			break;
		case MessageCommonData.headCmd_rsp.LOGIN_RSP:
			//登陆响应
			
			
			
			break;
		case MessageCommonData.headCmd_rsp.SELECT_ALARM_RSP:
			//查询告警信息响应
			
			
			
			break;
		case MessageCommonData.headCmd_rsp.SEND_DEVICE_RSP:
			//发送主机指令响应

			
			
			break;
		case MessageCommonData.headCmd_rsp.SELECT_DEVICE_STATE_RSP:
			//查询设备状态响应
			
			
			
			break;
		case MessageCommonData.headCmd_rsp.UPDATE_PWD_RSP:
			//修改密码响应
			
			
			break;

		}

	}

	
	/**
	 * 判断数据的合理性
	 * @param baseBean
	 * @return
	 */
	private boolean cheakeData(MessageBaseBean baseBean) {
		
		
		boolean state = true;
		
		//判断信令的版本号是否统一
		if(baseBean.messageVersion!=MessageCommonData.headCmd_commData.CMDVERSION){
			state = false;
			
		}
		
		if(baseBean.usrType!=MessageCommonData.usrType.MOBILE_USR){
			
			state = false;
		}
		
		
		return state;
	}

}
